#include <iostream>
#include "ExecutionLog.h"
#include "GenericKeys.h"
#include "General Utils.hpp"
#include "Vector2D.h"
#include <time.h>

using namespace std;

ExecutionLog* ExecutionLog::Instance()
{
	static ExecutionLog instance;
	return &instance;
}

void ExecutionLog::Init()
{
	string filename;
	filename += "Logs\\";

	char date[9], time[9];
	_strdate(date);
	_strtime(time);
	string sDate = date;
	string sTime = time;

	sDate.replace(2, 1, ".");
	sDate.replace(5, 1, ".");
	filename += sDate;
	filename += " at time ";
	sTime.replace(2, 1, "-");
	sTime.replace(5, 1, "-");
	filename += sTime;
	filename += ".txt";
	executionLog.open(filename.c_str());
	m_MessageNum = 0;

	this->AddMessage("Compiled on " + GetString(__TIMESTAMP__));

}

void ExecutionLog::CloseFile()
{
	executionLog.close();
}

void ExecutionLog::AddMessage(string message, OGL_Color color)
{
	++m_MessageNum;
	string completeMessage = GetString(m_MessageNum) + ": " + message;
	m_Log.push_front(completeMessage);

	m_Colors.push_front(color);

	cout << completeMessage << endl;
	executionLog << completeMessage << endl;
}

void ExecutionLog::AddMessage(char* message, OGL_Color color)
{
	string m_Message = message;
	AddMessage(m_Message, color);
}
void ExecutionLog::Display()
{
	list<string>::iterator iter = m_Log.begin();
	list<OGL_Color>::iterator colorIter = m_Colors.begin();
	Coords location = Coords(50, 100);

	int num = 0; // The number of messages currently displayed on the screen.
	int limit = 5; // The limit of the number of messages to display.

	while(num < limit && iter != m_Log.end())
	{
		Key->GetFont("onuava")->drawText(location.x, location.y, (*iter), (*colorIter));
		location.y += 20;
		iter++;
		colorIter++;
		num++;
	}
}